﻿using System;
using System.Collections.Generic;
using AutoMapper;
using WeiXinPF.Modules.CDRT.SmartCity.Application.DTO;
using WeiXinPF.Modules.CDRT.SmartCity.Domain.Models;
using WeiXinPF.Modules.CDRT.SmartCity.Infrastructure.Repositories;
using System.Linq;

namespace WeiXinPF.Modules.CDRT.SmartCity.Application.Services
{
    public class MessageService : IMessageService
    {
        private readonly IMessageRepository _msgRepository;

        public MessageService()
        {
            var context = new SmartContext();
            _msgRepository = new MessageRepository(context);

            Mapper.CreateMap<Message, MessageDTO>();
            Mapper.CreateMap<MessageDTO, Message>()
                 .ForMember(msg => msg.create_time, dto => { dto.Ignore(); });
        }

        public void CreateMessage(MessageDTO dto)
        {
            //将dto转化为entity
            var message = (Message)Mapper.Map(dto, typeof(MessageDTO), typeof(Message));
            message.create_time = DateTime.Now;
            message.update_time = DateTime.Now;
            _msgRepository.Add(message);
            _msgRepository.SaveChange();
        }

        public MessageDTO GetMessage(int id)
        {
            var message = _msgRepository.Get(id);
            if (message == null)
            {
                return null;
            }

            return Mapper.Map<MessageDTO>(message);
        }


        #region 排序
        private static IList<T> IListOrderBy<T>(IList<T> list, string propertyName) where T : new()
        {
            if (list == null || list.Count == 0) return list;
            Type elementType = list[0].GetType();
            System.Reflection.PropertyInfo propertyInfo = elementType.GetProperty(propertyName);

            System.Linq.Expressions.ParameterExpression parameter = System.Linq.Expressions.Expression.Parameter(elementType,"");
            System.Linq.Expressions.Expression body = System.Linq.Expressions.Expression.Property(parameter,propertyInfo);
            
            System.Linq.Expressions.Expression sourceExpression = list.AsQueryable().Expression;
           Type sourcePropertyType = propertyInfo.PropertyType;

            System.Linq.Expressions.Expression lamdba = System.Linq.Expressions.Expression.Call(typeof(System.Linq.Queryable), "OrderBy", 
                new Type[] { elementType, sourcePropertyType },
                sourceExpression, System.Linq.Expressions.Expression.Lambda(body, parameter));
            return list.AsQueryable().Provider.CreateQuery<T>(lamdba).ToList<T>();
        }
        #endregion


        public List<MessageDTO> GetMessages(int wid, string keyWord, int status, string type,string order)
        {
            //测试，以title为例
            var messages = _msgRepository.GetAllList(p => p.wid.Value.Equals(wid),p=>p.title,false);
            List<MessageDTO> messageDTOs = new List<MessageDTO>();
            foreach (var message in messages)
            {
                if ((string.IsNullOrEmpty(keyWord) || message.content.Contains(keyWord)) && message.status == status && (message.type_ == type || type == "全部"))
                {

                    messageDTOs.Add(
                        (MessageDTO)Mapper.Map(message, typeof(Message), typeof(MessageDTO)));
                }
            }

            return messageDTOs;
        }


        public List<MessageDTO> GetMessages(int wid, string keyWord , int status,string type)
        {
            
            var messages = _msgRepository.GetAllList(p => p.wid.Value.Equals(wid));
            List<MessageDTO> messageDTOs = new List<MessageDTO>();
            foreach(var message in messages)
            {
                if ((string.IsNullOrEmpty(keyWord) || message.content.Contains(keyWord) )&& message.status == status && (message.type_ == type || type == "全部"))
                {
                    
                    messageDTOs.Add(
                        (MessageDTO)Mapper.Map(message, typeof(Message), typeof(MessageDTO)));
                }
            }
            return messageDTOs;
        }

        public List<MessageDTO> GetAllMessages(int wid, string keyWord,string type)
        {
            var messages = _msgRepository.GetAllList(p => p.wid.Value.Equals(wid));
            List<MessageDTO> messageDTOs = new List<MessageDTO>();
            foreach (var message in messages)
            {
                if ((string.IsNullOrEmpty(keyWord) || message.content.Contains(keyWord))&&(message.type_ == type || type=="全部"))
                {
                    messageDTOs.Add(
                        (MessageDTO)Mapper.Map(message, typeof(Message), typeof(MessageDTO)));
                }
            }
            return messageDTOs;
        }

        public void DeleteMessage(int messageid)
        {
            _msgRepository.Delete(messageid);
            _msgRepository.SaveChange();
        }

        public void UpdateMessage(Message msg)
        {
            _msgRepository.Modify(msg);
            _msgRepository.SaveChange();
        }

    }
}
